const express = require('express');
const router = express.Router();
const query = require('../config/db');

router.get('/course', async (req, res, next) => {
    // 获取前端传递的数据
    let { cid } = req.query;
    // 通过cid查询数据
    let sql = `select * from e_course where cid='${cid}'`;
    let result = await query(sql);

    // 课程详情
    // 如果是空数组,代表没有这条数据
    if (result.length == 0) {
        next('该数据不存在');
        return;
    }
    // 有数据
    let course = result[0];

    // 老师数据
    let sql2 = `select * from e_teacher where tid='${course.tid}'`;
    let result2 = await query(sql2);
    let teacher = result2[0];//{id:"",tid:""}

    // 视频数据
    let sql3 = `select * from e_video where cid='${course.cid}' order by vnum`
    let video = await query(sql3);

    // 返回数据
    res.send({
        success: true,
        msg: "查询成功",
        status: 200,
        data: {
            //课程信息
            course,
            //老师的数据
            teacher,
            //视频数据
            video
        }
    })
})

module.exports = router;